package com.Txunda.fxw.ui.activity.other;

import java.util.ArrayList;
import java.util.Map;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.L;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.fgt.WebAty;
import com.Txunda.fxw.ui.http.Office;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * ====================================
 * 作者：DUKE_HwangZj
 * 日期：2018/11/15
 * 时间：0:30
 * 描述：修改为律师诉讼保全
 * 修改：
 * 说明:
 * ====================================
 */
public class LawServiceAty extends BaseAty {
    @ViewInject(R.id.aty_title3_tv_center)
    private TextView aty_title3_tv_center;
    @ViewInject(R.id.lv_LawServiceAty)
    private ListView lv_LawServiceAty;
    private String district_name;
    private String district_id3;
    private String area_name;
    private Office office;
    private MyAdapter adp;

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_law_service;
    }

    @Override
    protected void initialized() {
        office = new Office();
        aty_title3_tv_center.setText("律师诉讼保全");
        area_name = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_mode);
        PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_district);
        district_name = getIntent().getStringExtra("district_name");
        district_id3 = getIntent().getStringExtra("district_id3"); // 地区id

    }

    @Override
    protected void requestData() {
        showProgressDialog();
        if (TextUtils.isEmpty(district_name)) {
            office.preservationlist(district_id3, "0", area_name, this);
        } else {
            if (!district_id3.equals("121")) {
                office.preservationlist(district_id3, "1", district_name, this);
            } else {
                office.preservationlist(district_id3, "0", district_name, this);
            }
        }

        adp = new MyAdapter();
        lv_LawServiceAty.setAdapter(adp);
        lv_LawServiceAty.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, String> item = adp.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putString("type", "fb");
                bundle.putString("class_id", item.get("id"));
                bundle.putString("url", item.get("url"));
                startActivity(WebAty.class, bundle);
//                Bundle bundle = new Bundle();
//                bundle.putString("district", TextUtils.isEmpty(district_name) ? area_name : district_name);
//                bundle.putString("id", list_data.get(position).get("id"));
//                startActivity(LawServiceDetailAty.class, bundle);
            }
        });
    }

    Map<String, String> data;
    ArrayList<Map<String, String>> list_data = new ArrayList<Map<String, String>>();

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        data = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(data))
            return;
        list_data = JSONUtils.parseKeyAndValueToMapList(data.get("data"));
        adp.notifyDataSetChanged();
    }

    /**
     * @author txunda_lh 2017年6月3日10:30:04
     */

    class MyAdapter extends BaseAdapter {

        MyViewHolder vh;

        @Override
        public int getCount() {
            return list_data.size();

        }

        @Override
        public Map<String, String> getItem(int position) {
            return list_data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(LawServiceAty.this).inflate(R.layout.item_company_list, parent, false);
                vh = new MyViewHolder();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (MyViewHolder) convertView.getTag();
            }

            Glide.with(LawServiceAty.this).load(list_data.get(position).get("cover_pic")).into(vh.item_company_img);
            vh.item_company_title.setText(list_data.get(position).get("name"));

            return convertView;
        }

        class MyViewHolder {
            @ViewInject(R.id.compmay_frame_item)
            private FrameLayout compmay_frame_item;
            @ViewInject(R.id.item_company_img)
            private ImageView item_company_img;
            @ViewInject(R.id.item_company_title)
            private TextView item_company_title;
            @ViewInject(R.id.item_company_distance)
            private TextView item_company_distance;
            @ViewInject(R.id.item_company_address)
            private TextView item_company_address;
        }

    }


}
